
//
//  SopViewController.m
//  DigiOps
//
//  Created by app on 2018/7/18.
//  Copyright © 2018年 俞渊华. All rights reserved.
//

#import "SopViewController.h"
#import "SopViewCell.h"
#import "SopOptionViewCell.h"
#import "DetailViewController.h"
#import "SopOptionViewController.h"
#import "SopTextViewCell.h"
#import "SopPhotoViewCell.h"
#import "SopAddPicViewController.h"
#import "SopTextViewController.h"
#import "WBPopMenuModel.h"
#import "WBPopMenuSingleton.h"
#import "WBPopMenuView.h"
#import "SopNotokViewController.h"
#import "UserSites.h"
#import "SopModel.h"
#import "PointModel.h"
#import "PointModelOffline.h"
#import "PointValueModel.h"

@interface SopViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *sopArray;

@property(nonatomic, assign)NSInteger pageSize;
@property(nonatomic, assign)NSInteger pageIndex;
@property(nonatomic, strong)UILabel* titleLabelb;
@property(nonatomic, strong)NSString* seq;
@property(nonatomic, strong)NSString* sopUuid;
@property(nonatomic, strong)NSString* sopPicUrl;
@property(nonatomic, strong)NSString* pointUuid;

@end

@implementation SopViewController
static SopViewController *svc = nil;

+ (instancetype)share {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        svc = [[self alloc] init];
    });
    return svc;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageSize = 20;
    self.pageIndex = 1;
    self.sopArray = [NSMutableArray array];
    [self.view addSubview:self.tableView];
    
    CGRect labelFrame = CGRectMake(0.0, 0.0, SCREEN_WIDTH, 36.0);
    UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
    label.font = [UIFont boldSystemFontOfSize:18];
    label.numberOfLines = 2;
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = UITextAlignmentLeft;
    label.textColor = [UIColor whiteColor];
    //    label.shadowColor = [UIColor blackColor];
    //    label.shadowOffset = CGSizeMake(0.0, -1.0);
    label.lineBreakMode = UILineBreakModeMiddleTruncation;
    label.text=@"巡逻";
    self.navigationItem.titleView = label;
    
    UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleBordered target:self action:@selector(clickEvent)];
    [myButton setTintColor:[UIColor whiteColor]];
    if (![MySingleton sharedMySingleton].finished) {
//        self.navigationItem.rightBarButtonItem = myButton;
    }
    
//    //两个按钮的父类view
//
//    UIView *rightButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 50)];
//    //refresh按钮
//    UIButton *refreshBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 0, 50, 50)];
//
//    [rightButtonView addSubview:refreshBtn];
//
//    [refreshBtn setImage:[UIImage imageNamed:@"ic_autorenew"] forState:UIControlStateNormal];
//
//    [refreshBtn addTarget:self action:@selector(refreshBtnEvent) forControlEvents:UIControlEventTouchUpInside];
//
//    //提交按钮
//
//    UIButton *mainAndSearchBtn = [[UIButton alloc] initWithFrame:CGRectMake(100, 0, 50, 50)];
//
//    [rightButtonView addSubview:mainAndSearchBtn];
//
//    [mainAndSearchBtn setImage:[UIImage imageNamed:@"overflow_icon"] forState:UIControlStateNormal];
//
//    [mainAndSearchBtn addTarget:self action:@selector(mainAndSearchBtnEvent) forControlEvents:UIControlEventTouchUpInside];
//
//    //把右侧的两个按钮添加到rightBarButtonItem
//
//    UIBarButtonItem *rightCunstomButtonView = [[UIBarButtonItem alloc] initWithCustomView:rightButtonView];
//
//    self.navigationItem.rightBarButtonItem = rightCunstomButtonView;
    
    /** MJRefresh的block也需要用若引用 */
    WEAKSELF
    //去掉刷新
//    self.tableView.mj_header = [XMRefreshHeader xm_headerWithRefreshingBlock:^{
//        self.pageIndex = 1;
////        [self.sopArray removeAllObjects];
//        //        [weakSelf searchAticle];
//    }];
    //默认block方法：设置上拉加载更多
    //    NSArray * array = [WorkOrderModel findWhere:@[@"executeState",@"=",@(1)]];
    //调本地存储
        NSArray * array = [PointModel findWhere:@[@"uuid",@"=",_workModel.uuid]];
    
    NSMutableArray* Temp=[NSMutableArray new];
    for (PointModel* model in array) {
        if ([model.pointNum isEqualToString:self.line.pointNum]) {
            [Temp addObject:model];
        }
        
    }
    
    if (Temp.count>0) {
        [self.sopArray removeAllObjects];
        [self.sopArray addObjectsFromArray:Temp];
        
        //排序
        NSArray *sortArray = [self.sopArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            
            PointModel *pModel1 = obj1;
            PointModel *pModel2 = obj2;
            if ([pModel1.seq intValue] > [pModel2.seq intValue]) { //不使用intValue比较无效
                return NSOrderedDescending;//降序
            }else if ([pModel1.seq intValue] < [pModel2.seq intValue]){
                return NSOrderedAscending;//升序
            }else {
                return NSOrderedSame;//相等
            }
            
        }];
        [self.sopArray removeAllObjects];
        self.sopArray=[NSMutableArray arrayWithArray:sortArray];
        //赋值sopuuid
        for (PointModel* pm in Temp) {
            _sopUuid=pm.sopUuid;
            _pointUuid=pm.pointUuid;//
            self.titleLabelb.text=pm.offlineName;
            break;
        }
        
        [self.tableView reloadData];
    }
    else{//访问服务器
        if ([MySingleton sharedMySingleton].finished&&![[UserInfo share].role.powerType isEqualToString:@"1"]) {
            [self postFinishUI];
        }
        else{
        //离线存储
        NSArray * array1 = [PointModelOffline findWhere:@[@"offlineuuid",@"=",self.line.pointNum]];
        NSMutableArray* Temp1=[NSMutableArray new];
        for (PointModelOffline* model in array1) {
            if ([model.pointNum isEqualToString:self.line.pointNum]) {
                PointModel* aa=[PointModel new];
                aa.seq=model.seq;
                aa.title=model.title;
                aa.content=model.content;
                aa.shiftUuid=model.shiftUuid;
                aa.sopUuid=model.sopUuid;
                aa.auditUuid=model.auditUuid;
                aa.pointUuid=model.pointUuid;//pointUuid多个检查点
                aa.categoryName=model.categoryName;
                aa.categoryWeight=model.categoryWeight;
                aa.type=model.type;
                aa.valueArr=model.valueArr;
                aa.picture=model.picture;
                aa.remark=model.remark;
                aa.uuid=_workModel.uuid;//
                aa.pointNum=model.pointNum;
                aa.offlineName=model.offlineName;
                
                [Temp1 addObject:aa];
            }
            
        }
        if (Temp1.count>0) {
            [self.sopArray removeAllObjects];
            [self.sopArray addObjectsFromArray:Temp1];
            
            //排序
            NSArray *sortArray = [self.sopArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
                
                PointModel *pModel1 = obj1;
                PointModel *pModel2 = obj2;
                if ([pModel1.seq intValue] > [pModel2.seq intValue]) { //不使用intValue比较无效
                    return NSOrderedDescending;//降序
                }else if ([pModel1.seq intValue] < [pModel2.seq intValue]){
                    return NSOrderedAscending;//升序
                }else {
                    return NSOrderedSame;//相等
                }
                
            }];
            [self.sopArray removeAllObjects];
            self.sopArray=[NSMutableArray arrayWithArray:sortArray];
            //赋值sopuuid
            for (PointModel* pm in Temp1) {
                _sopUuid=pm.sopUuid;
                _pointUuid=pm.pointUuid;//
                self.titleLabelb.text=pm.offlineName;
                break;
            }
            
            [self.tableView reloadData];
        }
        else{
            if ([MySingleton sharedMySingleton].finished) {
                [self postFinishUI];
            }
            else
            {
                [self postUI];
            }
        }
        }
    }
    //返回
    self.navigationItem.hidesBackButton = YES;
    UIImage *image = [UIImage imageNamed:@"Back"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 30, 44);
    backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [backButton setImage:image forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftItemsSupplementBackButton = YES;
}
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
}
-(void)goBack
{
    [self clickEvent];
}

-(void)searchAticle{
    
}
-(void)submitSop
{
    //去除乱码
//    for (PointModel* pm in self.sopArray) {
//        if ([pm.content hasPrefix:@"[{"]) {
//            if (pm.valueArr.count>0) {
//            PointValueModel* a=pm.valueArr[0];
//            pm.content=a.value;
//            }
//        }
//        if ([pm.content isEqualToString:@"[]"]) {
//            pm.content=@"";
//        }
//    }
    //执行代理方法
    if ([_delegate respondsToSelector:@selector(SopToRouteTranfer:pointNum:)]) {
        [_delegate SopToRouteTranfer:self.sopArray pointNum:self.line.pointNum];
    }
    NSArray * array = [PointModel findWhere:@[@"uuid",@"=",_workModel.uuid]];
    if (array.count>0) {
        //删除sopuuid的pointmodel
        for (PointModel* pm in array) {
            if (_pointUuid)
                [PointModel deleteWhere:@[@"uuid",@"=",_workModel.uuid,@"pointUuid",@"=",_pointUuid]];
        }
    }
    for (PointModel* pm in self.sopArray) {
        [pm saveOrUpdateAsync:^(BOOL isSuccess) {
            
        }];
    }
}
-(void)postUI{
    UserSites* s=[UserSites mj_objectWithKeyValues:[UserInfo share].sites[0]];
    //离线调用去掉餐宿
    NSDictionary *params = @{
                             @"siteUuid" : self.line.pointUuid
                             };
    [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    [HttpTool get:[NSString stringWithFormat:@"digiops/app/SelectPoints"] params:nil success:^(id responseObj) {
        NSLog(@"");
        [self.sopArray removeAllObjects];

        for (NSDictionary* sop in responseObj[@"content"]) {
            SopModel* s=[SopModel mj_objectWithKeyValues:sop];

            if ([self.line.pointNum isEqualToString:s.pointNum]) {
                self.titleLabelb.text=s.sopName;
            for (NSDictionary* temp in s.sopTemplates) {
                
                PointModel* p=[PointModel mj_objectWithKeyValues:temp];

                NSData *data = [p.content dataUsingEncoding:NSUTF8StringEncoding];
                NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                NSMutableArray* arr=[NSMutableArray new];
                for (NSDictionary* v in dict) {
                    PointValueModel* model=[PointValueModel mj_objectWithKeyValues:v];
                    [arr addObject:model];
                    
                }
                p.valueArr=arr;
                p.sopUuid=s.uuid;
                _sopUuid=s.uuid;
                p.uuid=_workModel.uuid;//没处理pointUuid
                p.pointNum=s.pointNum;
                [self.sopArray addObject:p];

                NSLog(@"");
            }
            }
            NSLog(@"");
        }
        //离线存储
        for (NSDictionary* sop in responseObj[@"content"]) {
            SopModel* s=[SopModel mj_objectWithKeyValues:sop];
            
            for (NSDictionary* temp in s.sopTemplates) {
                
                PointModelOffline* p=[PointModelOffline mj_objectWithKeyValues:temp];
                
                NSData *data = [p.content dataUsingEncoding:NSUTF8StringEncoding];
                NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                NSMutableArray* arr=[NSMutableArray new];
                for (NSDictionary* v in dict) {
                    PointValueModel* model=[PointValueModel mj_objectWithKeyValues:v];
                    [arr addObject:model];
                    
                }
                p.valueArr=arr;
                p.sopUuid=s.uuid;
                p.offlineuuid=s.pointNum;
                p.pointNum=s.pointNum;//没处理pointUuid
                [p saveOrUpdateAsync:^(BOOL isSuccess) {
                    
                }];
                NSLog(@"");
            }
            NSLog(@"");
            
        }
        NSLog(@"");
        [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
        //        [UITool showMBProgressHud:[UIApplication sharedApplication].keyWindow withText:@"登录失败，稍后再试" withTime:1.0];
        
        //        [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
        
    }];
}

-(void)postFinishUI{
    UserSites* s=[UserSites mj_objectWithKeyValues:[UserInfo share].sites[0]];
    
    NSDictionary *params = @{
                             @"uuid" : _workModel.uuid,
                             @"workOrderType" : @(_workModel.workOrderType)
                             };
    [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    [HttpTool post:[NSString stringWithFormat:@"digiops/app/workOrder/SelectFindById"] params:params success:^(id responseObj) {
        NSLog(@"");
        if (responseObj[@"content"]==[NSNull null]) {
            [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
            return;
        }
        [self.sopArray removeAllObjects];
        for (NSDictionary* shift in responseObj[@"content"]) {
            SopModel* s=[SopModel mj_objectWithKeyValues:shift];
            
            if ([self.line.pointNum isEqualToString:s.pointName]) {
                
                for (NSDictionary* shift1 in shift[@"recordContents"]) {
                    
                    PointModel* a=[PointModel mj_objectWithKeyValues:shift1];
                    if ([a.templateUuid isEqualToString:s.sopUuid]) {
                        
                        NSLog(@"");
                        //                    p.shiftUuid=a.uuid;
                        [self.sopArray addObject:a];
                    }
                }
            }
            
        }
        [self.tableView reloadData];
        NSLog(@"");
        [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
        //        [UITool showMBProgressHud:[UIApplication sharedApplication].keyWindow withText:@"登录失败，稍后再试" withTime:1.0];
        
        //        [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
        
    }];
}
-(void)refreshBtnEvent{
    
}
-(void)mainAndSearchBtnEvent{
    [self clickEvent];
}
-(void)clickEvent{
    [self submitSop];
    [self.rt_navigationController popViewControllerAnimated:YES complete:nil];
//    NSMutableArray *obj = [NSMutableArray array];
//
//    for (NSInteger i = 0; i < [self titles].count; i++) {
//
//        WBPopMenuModel * info = [WBPopMenuModel new];
//        //        info.image = [self images][i];
//        info.title = [self titles][i];
//        [obj addObject:info];
//    }
//
//    [[WBPopMenuSingleton shareManager]showPopMenuSelecteWithFrame:150
//                                                             item:obj
//                                                           action:^(NSInteger index) {
//                                                               NSLog(@"index:%ld",(long)index);
//                                                               [self submitSop];
//                                                           }];
}
- (NSArray *) titles {
    return @[@"提交巡逻SOP"];
}

- (NSArray *) images {
    return @[@"right_menu_QR@3x",
             @"right_menu_addFri@3x",
             @"right_menu_multichat@3x",
             @"right_menu_sendFile@3x",
             @"right_menu_facetoface@3x",
             @"right_menu_payMoney@3x"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.sopArray.count;
//    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PointModel* model=(PointModel*)[self.sopArray objectAtIndex:indexPath.row];
    if ([model.type isEqualToString:@"select"]) {
        SopOptionViewCell* cell=[SopOptionViewCell cellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.pointTitle.text=[NSString stringWithFormat:@"%@.%@",model.seq, model.title];
        //自动折行设置
        cell.pointTitle.lineBreakMode = UILineBreakModeWordWrap;
        cell.pointTitle.numberOfLines = 0;
        if ([MySingleton sharedMySingleton].finished)
            [cell.okButton setTitle:model.content forState:UIControlStateNormal];
        if ([model.content hasPrefix:@"["])
        {
            NSMutableArray* value=[NSMutableArray new];
            for (PointValueModel* p in model.valueArr) {
                [value addObject:p.value];
            }
            if (value.count>0)
                [cell.okButton setTitle:value[0] forState:UIControlStateNormal];
        }
        else
            [cell.okButton setTitle:model.content forState:UIControlStateNormal];
        cell.block =^(UIButton* sender){
            if ([MySingleton sharedMySingleton].finished)
                return;
            self.seq=model.seq;

            SopOptionViewController* detailVC = [[SopOptionViewController alloc]init];
                detailVC.model = model;
            detailVC.block = ^(NSString *txt) {
                [cell.okButton setTitle:txt forState:UIControlStateNormal];
                NSMutableArray* sopTemp=[NSMutableArray new];
                for (PointModel* model in self.sopArray) {
                    if ([model.seq isEqualToString:self.seq]) {
                        model.content=txt;
                    }
                    [sopTemp addObject:model];
                }
                [self.sopArray removeAllObjects];
                self.sopArray=sopTemp;
            };
            [self.rt_navigationController pushViewController:detailVC animated:YES];
        };
        return cell;
    }
    else if([model.type isEqualToString:@"text"]){
        self.seq=model.seq;

        SopTextViewCell* cell=[SopTextViewCell cellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.pointTitle.text=[NSString stringWithFormat:@"%@.%@",model.seq, model.title];
        //自动折行设置
        cell.pointTitle.lineBreakMode = UILineBreakModeWordWrap;
        cell.pointTitle.numberOfLines = 0;
        if ([MySingleton sharedMySingleton].finished)
            cell.textField.text=model.content;
        if ([model.content isEqualToString:@"[]"]){
            cell.textField.text=@"请在此处输入";
            [cell.textField setTextColor:[UIColor lightGrayColor]];
        }
        else{
            cell.textField.text=model.content;
            [cell.textField setTextColor:[UIColor blackColor]];
        }
        cell.block = ^{
            if ([MySingleton sharedMySingleton].finished)
                return;
            self.seq=model.seq;

            SopTextViewController * detailVC = [[SopTextViewController alloc]init];
            //    DetailViewController * detailVC = [[DetailViewController alloc]init];
            //    detailVC.model = self.aticleArray[indexPath.row];
            //已保存的文本要在下一页显示
            detailVC.saveText=cell.textField.text;
            detailVC.block = ^(NSString *txt) {
                if (![txt isEqualToString:@""]) {

                cell.textField.text=txt;
                    [cell.textField setTextColor:[UIColor blackColor]];

                NSMutableArray* sopTemp=[NSMutableArray new];
                for (PointModel* model in self.sopArray) {
                    if ([model.seq isEqualToString:self.seq]) {
                        model.content=txt;
                    }
                    [sopTemp addObject:model];
                }
                [self.sopArray removeAllObjects];
                self.sopArray=sopTemp;
                }
                else{
                    cell.textField.text=@"请在此处输入";
                    [cell.textField setTextColor:[UIColor lightGrayColor]];
                }
                //                self.commentTxt=txt;
            };
            if (![MySingleton sharedMySingleton].finished)
                [self.rt_navigationController pushViewController:detailVC animated:YES];
        };
        return cell;
    }
    else if ([model.type isEqualToString:@"picture"]){
        self.seq=model.seq;

        SopPhotoViewCell* cell=[SopPhotoViewCell cellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.pointTitle.text=[NSString stringWithFormat:@"%@.%@",model.seq, model.title];
        //自动折行设置
        cell.pointTitle.lineBreakMode = UILineBreakModeWordWrap;
        cell.pointTitle.numberOfLines = 0;
//        if ([MySingleton sharedMySingleton].finished)
//            [cell.okButton setImage:[UIImage imageNamed:@"ic_photo_camera_withimage"] forState:UIControlStateNormal];
        //
        if (![model.content hasPrefix:@"["]&&model.content.length>2) {
            NSArray* pic=[model.content componentsSeparatedByString:@","];
            if (pic.count>0) {
                [cell.okButton setImage:[UIImage imageNamed:@"ic_photo_camera_withimage"] forState:UIControlStateNormal];
            }
        }
        cell.block = ^(UIButton *sender) {
            self.seq=model.seq;
            SopAddPicViewController* detailVC = [[SopAddPicViewController alloc]init];
            detailVC.label=@"巡逻";
            //    detailVC.model = self.aticleArray[indexPath.row];
            [self.rt_navigationController pushViewController:detailVC animated:YES];
            if ([MySingleton sharedMySingleton].finished)
            {
                NSArray* pic=[model.content componentsSeparatedByString:@","];
                detailVC.scoreArray=[pic mutableCopy];
            }
            //返回需要显示图片
            if (![model.content hasPrefix:@"["]) {
                NSArray* pic=[model.content componentsSeparatedByString:@","];
                if (pic.count>0) {
                    detailVC.scoreArray=[pic mutableCopy];
                    [cell.okButton setImage:[UIImage imageNamed:@"ic_photo_camera_withimage"] forState:UIControlStateNormal];
                }
            }
            detailVC.block = ^{
                [cell.okButton setImage:[UIImage imageNamed:@"ic_photo_camera_withimage"] forState:UIControlStateNormal];
            };
            detailVC.blockPicurl = ^(NSString *picurl) {
                NSMutableArray* sopTemp=[NSMutableArray new];
                for (PointModel* model in self.sopArray) {
                    if ([model.seq isEqualToString:self.seq]) {
                        if (picurl.length>1)
                            model.content=picurl;
                        else
                            model.content=@"";
                    }
                    [sopTemp addObject:model];
                }
                [self.sopArray removeAllObjects];
                self.sopArray=sopTemp;
            };
        };
        return cell;
    }
    else
    {
        self.seq=model.seq;
        SopViewCell* cell=[SopViewCell cellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.pointTitle.text=[NSString stringWithFormat:@"%@.%@",model.seq, model.title];
        //自动折行设置
        cell.pointTitle.lineBreakMode = UILineBreakModeWordWrap;
        cell.pointTitle.numberOfLines = 0;
        if ([MySingleton sharedMySingleton].finished)
        {
            if ([model.content isEqualToString:@"正常"]) {
                [cell.okButton setSelected:YES];
            }
            else
            {
                [cell.notokButton setSelected:YES];
            }
        }
        if (model.content)
        {
            if ([model.content isEqualToString:@"正常"]) {
                [cell.okButton setSelected:YES];
            }
            else
            {
                [cell.notokButton setSelected:YES];
            }
        }
        if ([model.content hasPrefix:@"["]) {
            [cell.okButton setSelected:YES];
            [cell.notokButton setSelected:NO];
        }
        cell.block = ^{
            self.seq=model.seq;
            SopNotokViewController* detailVC = [[SopNotokViewController alloc]init];
            //    detailVC.model = self.aticleArray[indexPath.row];
            detailVC.model=model;
            detailVC.shift=NO;
            [self.rt_navigationController pushViewController:detailVC animated:YES];
            detailVC.block = ^(NSString *txt) {
                NSMutableArray* sopTemp=[NSMutableArray new];
                for (PointModel* model in self.sopArray) {
                    if ([model.seq isEqualToString:self.seq]) {
                        //已存数据库备注为空问题
                        if (txt.length>0)
                        {
                        model.remark=txt;
                        model.content=@"不正常";
                        }
                    }
                    [sopTemp addObject:model];
                }
                [self.sopArray removeAllObjects];
                self.sopArray=sopTemp;
            };
            detailVC.blockPic = ^(NSString *txt) {
                NSMutableArray* sopTemp=[NSMutableArray new];
                for (PointModel* model in self.sopArray) {
                    if ([model.seq isEqualToString:self.seq]) {
                        //已存数据库图片为空问题
                        if (txt.length>1)
                            model.picture=txt;
                        model.content=@"不正常";
                    }
                    [sopTemp addObject:model];
                }
                [self.sopArray removeAllObjects];
                self.sopArray=sopTemp;
            };
        };
        cell.blockOK = ^{
            if ([MySingleton sharedMySingleton].finished)
                return;
            self.seq=model.seq;
            NSMutableArray* sopTemp=[NSMutableArray new];
            for (PointModel* model in self.sopArray) {
                if ([model.seq isEqualToString:self.seq]) {
                    model.content=@"正常";
                }
                [sopTemp addObject:model];
            }
            [self.sopArray removeAllObjects];
            self.sopArray=sopTemp;
        };
        return cell;
    }
//    cell.model=[RouteModel new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    AticleModel * model = self.aticleArray[indexPath.row];
    //    if (model.ViewType == 1) {
    //        return [HomeBaseCell heightWithModel:model];
    //
    //    }else if (model.ViewType == 2) {
    //        return [HomeOneImageCell heightWithModel:model];
    //
    //    }else if (model.ViewType == 3){
    //        return [NewsMoreImageCell heightWithModel:model];
    //    }else if (model.ViewType == 4){
    //        return [HomeOneImageCell heightWithModel:model];
    //    }
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    /*
     NewsDetailViewController * detailVC = [[NewsDetailViewController alloc]init];
     detailVC.model = self.aticleArray[indexPath.row];
     [self.rt_navigationController pushViewController:detailVC animated:YES];
     */
//    SopTextViewController * detailVC = [[SopTextViewController alloc]init];
//    DetailViewController * detailVC = [[DetailViewController alloc]init];
    //    detailVC.model = self.aticleArray[indexPath.row];
//    [self.rt_navigationController pushViewController:detailVC animated:YES];
}


- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableFooterView = [UIView new];
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
        view.backgroundColor = XMColor(241, 245, 247);
        UILabel* titleLabel = [[UILabel alloc]init];
        titleLabel.font = [UIFont xm_medium:17.0];
        titleLabel.numberOfLines = 1;
        titleLabel.textColor = [UIColor xm_jetblackColor];
        titleLabel.text=@"SOP";
        
        [view addSubview:titleLabel];
        self.titleLabelb=titleLabel;
        tableView.tableHeaderView=view;
        
        UIView * viewFoot = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
        viewFoot.backgroundColor = [UIColor whiteColor];
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        //        button.frame = CGRectMake(40, 100, SCREEN_WIDTH- 80, 30);
        button.backgroundColor = DGColor;
        [button setTitle:@"提交" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(submitRoute) forControlEvents:UIControlEventTouchUpInside];
//        [viewFoot addSubview:button];
        tableView.tableFooterView=viewFoot;
        //        [button setImage:[UIImage imageNamed:@"image"] forState:UIControlStateNormal];
        //        [button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        //        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view).offset(18);
            make.left.equalTo(view.mas_left).offset(20);
        }];
        
//        [button mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.center.equalTo(viewFoot).offset(0);
//            //            make.top.equalTo(view).offset(5);
//            make.width.mas_equalTo(SCREEN_WIDTH-80);
//            make.height.mas_equalTo(50);
//            //            make.left.equalTo(view.mas_left).offset(20);
//        }];
        _tableView = tableView;
    }
    return _tableView;
}
-(void)submitRoute
{
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
